package com.astoolkit.workflow.parsley
{
	import avmplus.getQualifiedClassName;
	
	import com.astoolkit.workflow.IPipelineTask;
	import com.astoolkit.workflow.IWorkflowTask;
	
	import mx.core.IMXMLObject;
	
	import org.spicefactory.parsley.core.context.Context;

	[DefaultProperty("task")]
	public class TaskParsleyTrigger implements IMXMLObject
	{
		[Inject]
		public var context : Context;
		
		public var messageType : Class;
		
		public var selector : String;
		
		public var taskInputProperty : Object;
		
		public var task : IPipelineTask;
		
		public function initialized( document : Object, id : String ) : void
		{
			
		}
		
		public function trigger( inMessage : Object ) : *
		{
			if( taskInputProperty is String )
			{
				if( !inMessage.hasOwnProperty( taskInputProperty ) )
					throw new Error( getQualifiedClassName( inMessage ) + " has no '" + taskInputProperty + "' property" );
				task.taskInput = inMessage[ taskInputProperty ];
			}
			else if( taskInputProperty is Function )
				task.taskInput = taskInputProperty( inMessage );
			task.begin();
		}
	}
}